package com.roadmap.client.sns;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Hibernate;

import com.roadmap.client.module.BaseManager;
import com.roadmap.common.BaseEntity;
import com.roadmap.entities.Visitor;
import com.roadmap.entities.ext.VisitorExt;
import com.roadmap.exception.ActionNotSupportedException;
import com.roadmap.exception.EntityNotFoundException;
import com.roadmap.exception.InvalidActionException;
import com.roadmap.exception.InvalidQueryException;
import com.roadmap.exception.InvalidStatusException;
import com.roadmap.exception.InvalidValueException;
import com.roadmap.exception.NullObjectException;
import com.roadmap.exception.ServerException;
import com.roadmap.repo.db.RdcQuery;

public class VisitorManager extends BaseManager {
	private final static int MAX_RESULTS = 10;
	
	public VisitorManager(String parentId) {
		super(parentId, Visitor.class);
	}

	@Override
	/** return all visitor records*/
	public List<VisitorExt> getEntitiesOfAspirant() throws NullObjectException,
			InvalidQueryException, ServerException, InvalidValueException {
		return getEntities(-1);
	}

	@Override
	/** return limited visitor records */
	public List getEntitiesOfParent() throws NullObjectException,
			InvalidQueryException, ServerException, InvalidValueException {
		return getEntities(MAX_RESULTS);
	}

	private List<VisitorExt> getEntities(int num)
			throws NullObjectException, InvalidQueryException, ServerException,
			InvalidValueException {
		String sql = "select {v.*}, a.realName from visitor as v, aspirant as a where v.hostId=:uid and v.visitorId=a.aspirantId order by visitTime DESC";
		RdcQuery query = getDbUtil().createNativeSQLQuery(sql);
		query.addEntity("v", Visitor.class);
		query.setParameter("uid", getOwnerId());
		query.addScalar("realName", Hibernate.STRING);
		if (num > 0) {
			query.setMaxResults(num);
		}
		List results = query.getResults();
		if (results != null && !results.isEmpty()) {
			List<VisitorExt> visitors = new ArrayList<VisitorExt>();
			Iterator iter = results.iterator();
			while (iter.hasNext()) {
				Object[] result = (Object[]) iter.next();
				Visitor vs = (Visitor) result[0];
				VisitorExt ext = new VisitorExt(vs);
				ext.setName((String) result[1]);
				visitors.add(ext);
			}
			return visitors;
		}
		return null;
	}

	@Override
	public void create(BaseEntity entity) throws NullObjectException,
			InvalidActionException, InvalidValueException, ServerException,
			EntityNotFoundException, ActionNotSupportedException,
			InvalidStatusException {
		if (!this.checkEntityIsValid(entity)) {
			return;
		}
		getDbUtil().saveOrUpdateEntity(entity);
		/*
		String hql = "from Visitor where id=? and hostId=?";
		Visitor vs = (Visitor) entity;
		Object[] params = {vs.getId(), vs.getHostId()};
		Object result = getDbUtil().getUnqiueResult(hql, params);
		if(result == null) {
			getDbUtil().save(entity);
		}
		else {
			getDbUtil().update(entity);
		}
		*/
	}

}
